package com.wh.minio.controller;

import com.wh.basic.vo.AjaxResult;
import com.wh.minio.service.IMinIoFileService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin(origins = "https://www.kongqichong.com:8080")
@RestController
@RequestMapping("/minio")
public class MinIoController {

    @Autowired
    private IMinIoFileService minIoFileService;

    //上传文件
    @RequestMapping("/upload")
    public AjaxResult upload(MultipartFile file) {
        String fileUrl = minIoFileService.upload(file);
        return AjaxResult.me().setResultObj(fileUrl);
    }

    /**
     * 多文件上传
     * @param files
     * @return
     */
    @RequestMapping("/uploads")
    public AjaxResult uploads(MultipartFile[] files) {
        List<String> fileUrls = new ArrayList<>();
        System.out.println(files);
        for (MultipartFile file : files) {
            String fileUrl = minIoFileService.upload(file);
            fileUrls.add(fileUrl);
            System.out.println("上传的文件是："+fileUrl);
        }
        return AjaxResult.me().setResultObj(fileUrls);
    }

    //删除文件
    @DeleteMapping("/{url}")
    public AjaxResult delete(@PathVariable("url") String url) {
        minIoFileService.delete(url);
        return AjaxResult.me().setMessage("删除成功!");
    }
}
